package com.zhijian.medical.service.impl;

import cn.hutool.core.date.DateUtil;
import com.zhijian.medical.dao.local.PersonProfessionalTechnologyEstimateMapper;
import com.zhijian.medical.entity.po.PersonProfessionalTechnologyEstimate;
import com.zhijian.medical.entity.vo.request.PersonProfessionalTechnologyEstimateSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.PersonProfessionalTechnologyEstimateSearchVo;
import com.zhijian.medical.entity.vo.response.PersonProfessionalTechnologyEstimateListVo;
import com.zhijian.medical.entity.vo.response.PersonProfessionalTechnologyEstimateVo;
import com.zhijian.medical.service.PersonProfessionalTechnologyEstimateService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 人员-专业技术能力评估表业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2022-03-27 12:30:33
 */
@Slf4j
@Service
public class PersonProfessionalTechnologyEstimateServiceImpl implements PersonProfessionalTechnologyEstimateService {

    @Autowired
    private PersonProfessionalTechnologyEstimateMapper mapper;

    @Override
    public PersonProfessionalTechnologyEstimate selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public PersonProfessionalTechnologyEstimateVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(PersonProfessionalTechnologyEstimate personProfessionalTechnologyEstimate) {
        return mapper.insert(personProfessionalTechnologyEstimate) > 0 ? personProfessionalTechnologyEstimate.getId() : 0L;
    }

    @Override
    public Long insert(PersonProfessionalTechnologyEstimateSaveOrUpdateVo personProfessionalTechnologyEstimateSaveOrUpdateVo) {
        PersonProfessionalTechnologyEstimate insertParam = new PersonProfessionalTechnologyEstimate();
        BeanUtils.copyProperties(personProfessionalTechnologyEstimateSaveOrUpdateVo, insertParam);
        if (StringUtils.isNotBlank(personProfessionalTechnologyEstimateSaveOrUpdateVo.getExamineDate())) {
            insertParam.setExamineDate(DateUtil.parseDate(personProfessionalTechnologyEstimateSaveOrUpdateVo.getExamineDate()));
        }
        return this.insert(insertParam);
    }

    @Override
    public boolean update(PersonProfessionalTechnologyEstimate personProfessionalTechnologyEstimate) {
        return mapper.updateByPrimaryKey(personProfessionalTechnologyEstimate) != 0;
    }

    @Override
    public boolean update(PersonProfessionalTechnologyEstimateSaveOrUpdateVo personProfessionalTechnologyEstimateSaveOrUpdateVo) {
        PersonProfessionalTechnologyEstimate updateParam = new PersonProfessionalTechnologyEstimate();
        BeanUtils.copyProperties(personProfessionalTechnologyEstimateSaveOrUpdateVo, updateParam);
        if (StringUtils.isNotBlank(personProfessionalTechnologyEstimateSaveOrUpdateVo.getExamineDate())) {
            updateParam.setExamineDate(DateUtil.parseDate(personProfessionalTechnologyEstimateSaveOrUpdateVo.getExamineDate()));
        }
        return this.update(updateParam);
    }

    @Override
    public Integer count(PersonProfessionalTechnologyEstimateSearchVo personProfessionalTechnologyEstimateSearchVo) {
        return mapper.count(personProfessionalTechnologyEstimateSearchVo);
    }

    @Override
    public List<PersonProfessionalTechnologyEstimateListVo> selectList(PersonProfessionalTechnologyEstimateSearchVo personProfessionalTechnologyEstimateSearchVo) {
        return mapper.selectList(personProfessionalTechnologyEstimateSearchVo);
    }

    @Override
    public Page<PersonProfessionalTechnologyEstimateListVo> selectPage(PersonProfessionalTechnologyEstimateSearchVo personProfessionalTechnologyEstimateSearchVo) {
        //先直接查总数
        int count = count(personProfessionalTechnologyEstimateSearchVo);
        //分页
        Page<PersonProfessionalTechnologyEstimateListVo> page = new Page<>(true, count, personProfessionalTechnologyEstimateSearchVo.getPageNum(), personProfessionalTechnologyEstimateSearchVo.getPageSize());
        personProfessionalTechnologyEstimateSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<PersonProfessionalTechnologyEstimateListVo> responseVos = this.selectList(personProfessionalTechnologyEstimateSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public PersonProfessionalTechnologyEstimateVo selectVoByPersonId(Long personId) {
        return mapper.selectVoByPersonId(personId);
    }

}